package com.mall.b2c.hessian.impl;

import com.google.common.collect.Lists;
import com.mall.b2c.Constants;
import com.mall.b2c.bean.StoreBean;
import com.mall.b2c.bean.StoreCityBean;
import com.mall.b2c.domain.store.StoreCityDomain;
import com.mall.b2c.domain.store.StoreDomain;
import com.mall.b2c.hessian.StoreHession;
import com.mall.b2c.service.StoreCityService;
import com.mall.b2c.service.StoreService;
import com.mall.common.exception.NoticeException;
import com.mall.common.hessian.impl.BaseHessianImpl;
import com.mall.common.hibernate.Finder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("b2cStoreHessian")
public class StoreHessionImpl extends BaseHessianImpl implements StoreHession{

    @Resource
    private StoreService storeService;
    @Resource
    private StoreCityService storeCityService;

    @Override
    public String getStoreInfo(Long id) {

        if(id == null ) {
            log.error("id="+id);
            throw new NoticeException("门店不存在");
        }
        StoreDomain  store = storeService.find(id);
        if(store == null){
            throw new NoticeException("会员不存在");
        }

        StoreBean bean = new StoreBean();
        bean.setId(store.getId());
        bean.setCityId(store.getCity().getId());
        bean.setCityName(store.getCity().getName());
        bean.setName(store.getName());

        StoreCityDomain city = storeCityService.find(store.getCity().getId());
        if(city != null){
            bean.setOthers(city.getStoreBean(bean));
        }
        return objToJson(bean);
    }

    @Override
    public String getStoreAll() {
        List<StoreDomain> stores = storeService.getAll();
        List<StoreBean> beans = Lists.newArrayList();
        for(StoreDomain store : stores){
            beans.add(store.getBean());
        }
        return listToJson(beans);
    }

    /***
     * 获取所有门店城市
     * @return
     */
    public String getStoreCitys(){
        List<StoreCityDomain> citys = storeCityService.getAll();
        List<StoreCityBean> beans = Lists.newArrayList();
        if(citys!=null){
            for(StoreCityDomain city : citys){
                beans.add(city.getBean());
            }
        }
        return listToJson(beans);
    }

    /***
     * 通过城市IP获取门店信息
     * @param cityId
     * @return
     */
    @Override
    public String getStoresByCityId(Long cityId){
        List<StoreDomain> stores = storeService.getList("city.id", cityId);
        List<StoreBean> beans = Lists.newArrayList();
        for(StoreDomain store : stores){
            beans.add(store.getBean());
        }
        return listToJson(beans);
    }

    /***
     * 通过城市名称获取门店
     * @param cityName
     * @return
     */
    public String getStoresByCityName(String cityName){
        List<StoreDomain> stores = storeService.getList("city.name", cityName);
        List<StoreBean> beans = Lists.newArrayList();
        for(StoreDomain store : stores){
            beans.add(store.getBean());
        }
        return listToJson(beans);
    }

    @Override
    public String getDefaultStore(String cityName) {
        Finder finder = Finder.create("from StoreDomain bean where bean.city.name like :cityName order by bean.priority desc limit 1");
        finder.setParam("cityName", cityName);
        StoreDomain store = (StoreDomain)finder.findUniqueByHQL();
        if(store == null){
            store = storeService.getDefaultStore();
        }
        StoreBean bean =  store.getBean();
        StoreCityDomain city = storeCityService.find(store.getCity().getId());
        if(city != null){
            bean.setOthers(city.getStoreBean(bean));
        }
        return objToJson(bean);
    }

    @Override
    public String getDefaultCity(String cityName) {

        StoreCityDomain city = storeCityService.get("name",cityName);
        if(city==null){
            city = storeCityService.get("is_default", true);
        }
        return objToJson(city.getBean());
    }

    @Override
    public String getDefaultCityById(Long cityId) {
        StoreCityDomain city = storeCityService.find(cityId);
        if(city==null){
            city = storeCityService.get("is_default", true);
        }
        return objToJson(city.getBean());
    }
}
